<?php

namespace G4T\Util;

class Highlighter
{
    const ENCODING = 'UTF-8';

    public static function highlight($content, array $words)
    {
        foreach ($words as $word) {
            $position = mb_stripos($content, $word, 0, self::ENCODING);
            $realWord = mb_substr($content, $position, mb_strlen($word, self::ENCODING), self::ENCODING);
            $highlightedWord = "<span class='highlighted'>$realWord</span>";
            if ($position) {
                $pre = mb_substr($content, 0, $position, self::ENCODING);
                $post = mb_substr($content, $position + mb_strlen($word, self::ENCODING), 50, self::ENCODING);
                $content = $pre . $highlightedWord . $post;

            }
        }
        return $content;
    }
}
